package com.easy.service.Impl;

import com.easy.bean.Fruit;
import com.easy.mapper.FruitMapper;
import com.easy.service.FruitService;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true) // 默认只读
public class FruitServiceImpl implements FruitService {
    @Autowired
    FruitMapper fruitMapper;
    @Override
    @Transactional(rollbackFor = Exception.class) // 写操作
    public Fruit add(Fruit fruit) {
        fruitMapper.add(fruit);
        Fruit result = fruitMapper.one(fruit.getFruit_id());
        return result;
    }


    //@Transactional//声明式事务
    @Override
    @Transactional(rollbackFor = Exception.class) // 写操作
    public int del(Integer id) {
        return fruitMapper.del(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class) // 写操作
    public Fruit edit(Fruit fruit) {
        fruitMapper.edit(fruit);
        Fruit result = fruitMapper.one(fruit.getFruit_id());
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class) // 写操作
    public List<Fruit> all(Fruit fruit) {
        return fruitMapper.all(fruit);
    }

    @Override
    @Transactional(rollbackFor = Exception.class) // 写操作
    public Fruit one(Integer id) {
        return fruitMapper.one(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class) // 写操作
    public void page(Page page, Fruit fruit) {
        int count=fruitMapper.pageCount(fruit);
        Object data=fruitMapper.page(page,fruit);
        page.setCount(count);
        page.setData(data);
    }
    @Override
    @Transactional(rollbackFor = Exception.class) // 写操作
    public void updateImageUrl(Integer fruitId, String imgUrl) {
        // 直接调用 FruitMapper 更新图片 URL
        fruitMapper.updateImageUrl(fruitId, imgUrl);
    }
}
